<template>
  <div class="login-container">
    <div class="login-box">
      <div
          style="padding: 50px 30px;background-color: white; margin-left: 100px; border-radius: 5px;box-shadow: 0 0 10px rgba(0,0,0,0.2)">
        <el-form ref="formRef" :rules="data.rules" :model="data.form" style="width: 300px">
          <div style="margin-bottom: 30px;font-size: 24px;text-align: center;color: #0742b1;font-weight: bold">欢 迎 注 册
          </div>
          <el-form-item prop="username">
            <el-input size="large" v-model="data.form.username" placeholder="请输入账号" :prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input size="large" v-model="data.form.password" placeholder="请输入密码" :prefix-icon="Lock"
                      show-password></el-input>
          </el-form-item>
          <el-form-item prop="register">
            <el-input size="large" v-model="data.form.register" placeholder="请确认密码" :prefix-icon="Connection"
                      show-password></el-input>
          </el-form-item>
          <div style="margin-bottom: 20px">
            <el-button @click="register" type="primary" size="large" style="width: 100%">注 册</el-button>
          </div>
          <div style="text-align: right">已有账号？请<a style="color: #0742b1;text-decoration: none" href="/login">登录</a>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/bgc.jpg");
  background-size: cover;
}

.login-box {
  width: 50%;
  height: 100%;
  display: flex;
  align-items: center;
  right: 0;
  position: absolute;
}
</style>

<script setup>
import {reactive, ref} from "vue";
import {User, Lock, Connection} from '@element-plus/icons-vue'
import request from "@/utils/request";
import {ElMessage} from "element-plus";

const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请再次输入密码'))
  } else if (value !== data.form.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const validateNoChinese=(rule, value, callback)=> {
  const chineseRegex = /[\u4e00-\u9fa5]/; // 用于匹配中文字符的正则表达式
  if (chineseRegex.test(value)) {
    callback(new Error('账号不能包含中文'));
  } else {
    callback();
  }
}


const data = reactive({
  form: {},
  rules: {
    username: [
      {required: true, message: '请输入账号', trigger: 'blur'},
      { validator: validateNoChinese, trigger: 'blur' }
    ],
    password: [
      {required: true, message: '请输入密码', trigger: 'blur'}
    ],
    register: [
      {validator: validatePass, trigger: 'blur'}
    ]
  }
})
const formRef = ref()

const register = () => {
  formRef.value.validate((valid) => {
      if(valid){
        request.post("/user/register", data.form).then(res => {
          if (res.code === '0') {
            ElMessage({
              type: 'success',
              message: '注册成功'
            })
            location.href="/login"
            data.form = {}
          } else {
            ElMessage({
              type: 'error',
              message: res.msg
            })
          }
        })
      }
  })
}


</script>
